#include <stdlib.h>
#include <stdio.h>

#include "cJSON_ext.h"


cJSON *cJSON_CreateLongLongNumber(int64_t num)
{
    cJSON *object = cJSON_CreateNumber(0);
    if (object == NULL)
        return NULL;

    object->rawvalue = malloc(26);
    if (object->rawvalue == NULL)
    {
        cJSON_Delete(object);
        return NULL;
    }

    snprintf(object->rawvalue, 26, "%lld", num);

    return object;
}


int64_t cJSON_GetValueInt64(cJSON * object)
{
    int64_t num;
    if (object == NULL)
        return 0;

    if (object->rawvalue)
    {
        num = atoll(object->rawvalue);
        return num;
    }

    num = (int64_t)object->valuedouble;
    return num;
}


cJSON *cJSON_AddLongLongNumberToObject(cJSON * const object, const char * const name, const int64_t number)
{
    cJSON *number_item = cJSON_CreateLongLongNumber(number);
    if (number_item == NULL)
        return NULL;

    cJSON_AddItemToObject(object, name, number_item);

    return number_item;
}
